////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	
	import kiwi.utils.Validation;
	
	import plantool.core.AirLauncher;
	import plantool.data.QueryResult;
	import plantool.events.OrderEvent;
	import plantool.modules.orders.data.Order;
	import plantool.modules.orders.data.Orderrow;
	import plantool.modules.users.data.UserHourTypes;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	[SWF(width="1280", height="768", frameRate="60")]
	public class Main extends AirLauncher {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _users:Array		= null;
		private var _currentUser:uint	= 0;
		private var _currentDay:uint	= 0;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function Main() {
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onComplete_createWeek():void {
			_currentDay = _currentDay + 7;
			if (_currentDay > 42) {
				_currentDay = 0;
				_currentUser++;
			}
			
			if (_currentUser < _users.length) {
				createWeek();
			}
		}
		
		private function createWeek():void {
			var d:Date 		= dateApi.getStartDate();
			d.date			-= 21;
			d.date			+= _currentDay;
			
			for (var i:uint = 0; i < dateApi.daysPerWeek; i++) {
				for (var j:uint = 0; j < dateApi.hoursPerDay; j++) {
					var from:Date	= new Date(d);
					from.hours		+= j;
					var to:Date		= new Date(from);
					to.hours		+= 1;
					
					var rnd:Number = Math.random();
					if (rnd > 0 && rnd <= .40) {
						var related_id:uint = 1 + (Math.floor(Math.random() * 14));
						userApi.saveUserHour(_users[_currentUser], from, to, UserHourTypes.USER_WORKPLACE, related_id, null, null, null);
					} else if (rnd > .4 && rnd <= .80) {
						userApi.saveUserHour(_users[_currentUser], from, to, UserHourTypes.USER_FREE, 0, null, null, null);
					}
				}
			}
			
			onComplete_createWeek();
		}
		
		private function onComplete_getUsers(e:QueryResult):void {
			_users = e.data;
			
			createWeek();
		}
		
		private function onComplete_saveOrder(e:QueryResult):void {
		}
		
		private function onComplete_getOrders(result:QueryResult):void {
			//show users
			if (Validation.isSet(result.data) && result.data.length > 0) {
				//create a row for each user
				for (var i:uint = 0; i < result.data.length; i++) {
					//get order
					var order:Order = result.data[i];
					
					//check if rows are loaded
					if (order.rowsLoaded) {
						createRows(order);
					}
						//else listen for even
					else {
						order.addEventListener(OrderEvent.ROWS_LOADED		, onOrder_rowsLoaded);
					}
				}
			}
		}
		
		private function onOrder_rowsLoaded(e:OrderEvent):void {
			//get order
			var order:Order = e.currentTarget as Order;
			
			//remove event
			order.removeEventListener(OrderEvent.ROWS_LOADED, onOrder_rowsLoaded);
			
			//create rows
			createRows(order);
		}
		
		private function createRows(order:Order):void {
			var d:Date 		= dateApi.getStartDate();
			d.date			-= 60;
			d.date			+= (order.id -1);
			for (var i:uint = 0; i < order.orderrows.length; i++) {
				orderApi.planOrderrow(order.orderrows[i], d, i + 1, null, null);
			}
		}
		
		private function onComplete_init(e:Event):void {
			//create 10 users
			var i:uint;
			
			
//			for (i = 0; i < 7; i++) {
//				var u:User 	= new User();
//				u.username	= "User " + (i + 1);
//				u.level		= 0;
//				u.color		= Math.random() * 0xFFFFFF;
//				u.password	= "test";
//				userApi.saveUser(u, null, null);
//			}
//			
//			//create 20 workplaces
//			for (i = 0; i < 15; i++) {
//				var w:Workplace 	= new Workplace();
//				w.name				= "Workplace " + (i + 1);
//				workplaceApi.saveWorkpace(w, null, null);
//			}
//			
//			
//			for (i = 0; i < 150; i++) {
//				var o:Order 		= new Order();
//				o.onr				= "O-" + (i + 1);
//				var numRows:uint	= 1 + Math.floor(Math.random() * 4);
//				for (var j:uint = 0; j < numRows; j++) {
//					var row:Orderrow 	= new Orderrow();
//					row.time			= 1 + Math.floor(Math.random() * 7);
//					o.orderrows.push(row);
//				}
//				
//				orderApi.saveOrder(o, null, null);
//			}
//			
//			orderApi.getOrders(onComplete_getOrders, null);
			
			
			//create user hours
			//userApi.getUsers(onComplete_getUsers, null);
		}
		
		protected override function init():void {
			base.stage.nativeWindow.maximize();
			addEventListener(Event.COMPLETE		, onComplete_init);
			super.init();
		}
	}
}